import request from '@/utils/request'
import {
	ref,
} from 'vue'

export default () => {
	const userInfoState = ref(null)

	const appts = ref({
		gappts: null,
		pappts: null
	})
	const sales = ref({
		gsales: null,
		psales: null
	})
	const activities = ref([])
	const signs = ref({
		gsigns: null,
		psigns: null
	})
	const getUserInfo = async (i) => {
		userInfoState.value = await request.get(`mini/userinfo`)
	}
	const getUserOpenid = async (code) => {
		userInfoState.value = await request.post(`mini/openid`, {
			code: code,
		})
	}
	const getUserAppt = async () => {
		appts.value = await request.get("mini/user/appt")
	}

	const getUserSale = async () => {
		sales.value = await request.get("mini/user/sale")
	}
	const getUserActivity = async () => {
		activities.value = await request.get("mini/user/activity")
	}
	const getUserSign = async () => {
		signs.value = await request.get("mini/user/sign")
	}

	const loadAdminToken = async (data) => {
		return await request.post("mini/partner/newtoken", data)
	}

	//用户的业务数据统计
	const loadUserBus = async () => {
		return await request.get("mini/user/bus")
	}

	const isAdmin = () => {
		let modal = uni.getStorageSync("user")
		if (!modal) {
			return false;
		}
		let user = JSON.parse(uni.getStorageSync("user"))
		if (user.Partners.length > 0) {
			return true;
		} else {
			return false;
		}
	}

	const isVipAdmin = () => {
		let modal = uni.getStorageSync("user")
		if (!modal) {
			return false;
		}
		let user = JSON.parse(uni.getStorageSync("user"))
		if (user.Partners.length > 0) {
			return user.Partners.some(item => item.partner_type == 2)
		} else {
			return false;
		}
	}

	const getUserOpt = async (mobile) => {
		return await request.post("mini/user/options", { mobile: mobile })
	}

	//管理员登录
	const userLogin = async (form) => {
		return await request.post("mini/auth/login", form)
	}
	
	//管理员登录
	const wechatLogout = async () => {
		return await request.get("mini/auth/logout")
	}
	
	const validcode = async (form) => {
		return await request.post("mini/auth/validcode", form)
	}
	//普通会员密码登录
	const userPasswordLogin = async (form) => {
		return await request.post("mini/passwordlogin", form)
	}

	const userResetPwd = async (form) => {
		return await request.post("mini/auth/reset", form)
	}
	return {
		getUserInfo,
		userInfoState,
		getUserAppt,
		getUserSale,
		getUserActivity,
		getUserSign,
		appts,
		sales,
		activities,
		signs,
		loadAdminToken,
		loadUserBus,
		isAdmin,
		userLogin,
		isVipAdmin,
		getUserOpt,
		userPasswordLogin,
		getUserOpenid,
		validcode,
		wechatLogout,
		userResetPwd
	}
}